package uestc.lj.gulishop.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import uestc.lj.common.utils.PageUtils;
import uestc.lj.common.utils.Query;
import uestc.lj.common.utils.R;
import uestc.lj.gulishop.ware.dao.WareInfoDao;
import uestc.lj.gulishop.ware.entity.WareInfoEntity;
import uestc.lj.gulishop.ware.feign.MemberFeignService;
import uestc.lj.gulishop.ware.service.WareInfoService;
import uestc.lj.gulishop.ware.vo.FareResponseVO;
import uestc.lj.gulishop.ware.vo.MemberAddressVO;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                new QueryWrapper<WareInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("id", key)
                    .or().like("name", key)
                    .or().like("address", key)
                    .or().like("areacode", key);
        }
        IPage<WareInfoEntity> page = this.page(new Query<WareInfoEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public FareResponseVO getFare(Long addrId) {
        FareResponseVO fareResponseVO = new FareResponseVO();
        R r = memberFeignService.info(addrId);
        if (r.getCode() == 0) {
            MemberAddressVO memberReceiveAddress = r.getData("memberReceiveAddress", new TypeReference<MemberAddressVO>() {
            });
            if (memberReceiveAddress != null) {
                fareResponseVO.setMemberAddressVO(memberReceiveAddress);

                fareResponseVO.setFare(new BigDecimal("52.5"));
                return fareResponseVO;
            }
        }
        return null;
    }
}